package net.zjitc.controller;


import net.zjitc.pojp.User;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.UserServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created by lenovo on 2017/11/11.
 */
public class UserController {
    UserService userService = new UserServiceImpl();

    @URLMapping(url = "/findAll")
    public String findAll(HttpServletRequest request, HttpServletResponse response) {
        List<User> list = userService.findAll();
        request.setAttribute("list", list);
        return "/admin/user/list.jsp";
    }

    @URLMapping(url = "/loginUI")
    public String loginUI(HttpServletRequest request, HttpServletResponse response) {

        return "/WEB-INF/user/login.jsp";
    }

    //进入注册页面
    @URLMapping(url = "/registUI")
    public String registUI(HttpServletRequest request, HttpServletResponse response) {
        return "/WEB-INF/user/regist.jsp";
    }

    //进行用户注册
    @URLMapping(url = "/regist")
    public String regist(HttpServletRequest request, HttpServletResponse response) {
        String inputCode = request.getParameter("code");
        String trueCode = (String) request.getSession().getAttribute("validateCode");
        if (!inputCode.equalsIgnoreCase(trueCode)) {
            request.setAttribute("codeMsg", "验证码错误");
            return "/WEB-INF/user/regist.jsp";
        }
        User user = new User();
        try {
            BeanUtils.populate(user, request.getParameterMap());
            int count = userService.save(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //登录激活提示页面
        request.setAttribute("msg", "注册成功，请先激活");
        return "/WEB-INF/user/msg.jsp";
    }

    @URLMapping(url = "/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) {
        String uid = request.getParameter("uid");
        userService.delete(uid);
        return "Redirect:" + request.getContextPath() + "/findAll";
    }

    @URLMapping(url = "/userLogin")
    public String userLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            List<User> list=userService.findAll();

            for(User u:list)
            {
                if(u.getUsername().equals(user.getUsername())&&u.getPassword().equals(user.getPassword()))
                {
                    if(u.getState()==0)
                    {
                        request.setAttribute("userMsg","尚未激活,请先激活");
                        return "/WEB-INF/user/login.jsp";
                    }else{
                        HttpSession session=request.getSession();
                        session.setAttribute("user",u);
                        String url=request.getContextPath()+"/index.jsp";
                        return "Redirect:"+url;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("userMsg","用户名或是密码错误");
        return "/WEB-INF/user/login.jsp";
    }

    @URLMapping(url = "/activeCode")
    public String activeCode(HttpServletRequest request, HttpServletResponse response){
        String code=request.getParameter("code");

        User user=userService.findByCode(code);
        if(user!=null){
            user.setCode(null);
            user.setState(1);
            int count=userService.update(user);
            request.setAttribute("msg","激活成功，请登录");

        }else{
            request.setAttribute("msg","激活失败，请先注册");
        }
        return "/WEB-INF/user/msg.jsp";
    }

    @URLMapping(url = "/userLogout")
    public String userLogout(HttpServletRequest request, HttpServletResponse response)
    {
        HttpSession session=request.getSession();
        session.removeAttribute("user");
        return "/WEB-INF/user/login.jsp";
    }

}

